import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * ClassName NewKnowledge
 * Description TODO
 * Author Yanyan
 * Date 2025/11/6 13:51
 * Version 1.0
 */
public class NewKnowledge {
    public static void main(String[] args) {
        int[][] array = {{1, 2, 3}, {4, 5, 6}};
        System.out.println(array[1][1]);

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }

        int[][] array2 = new int[][] {{1, 2, 3}, {4, 5, 6}};

        //Java中二维数组 列可以省略
        int[][] array3 = new int[2][];

    }

    //找和为某个值的两个数字
    public static int[] twoIndex(int[] array,int target) {
        int[] tmp = {-1, -1,};
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if(array[i] + array[j] == target) {
                    tmp[0] = i;
                    tmp[1] = j;
                }
            }
        }
        return tmp;
    }

    //找只出现了一次的数字
    public static int singleNum(int[] array) {
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }

    //判断是否有三个连续的奇数
    public static boolean function(int[] array) {

        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] % 2 != 0) {
                count++;
                if(count == 3) {
                    return true;
                }
            } else {
                count = 0;
            }
        }
        return false;
    }

    public static void main12(String[] args) {
        int[] array = {1, 3, 4, 6, 7, 3, 5, 4, 6};
        boolean flag = function(array);
        System.out.println(flag);

        int[] array2 = {1, 1, 4, 5, 1, 4};
        int ret = singleNum(array2);
        System.out.println(ret);

        int[] array3 = {1, 3, 5, 76, 7, 11, 54, 3};
        int[] ret2 = twoIndex(array3,18);
        System.out.println(Arrays.toString(ret2));
    }

    //奇数排偶数前面
    public static void func(int[] array) {
        int i = 0;
        int j = array.length - 1;
        while(i < j) {
            //找到排在前面的偶数
            //i < j需要每次判断，否则纯偶数或奇数的数组就会越界访问
            while(i < j && array[i] % 2 != 0) {
                i++;
            }
            //找到排在后面的奇数
            while(i < j && array[j] % 2 == 0) {
                j--;
            }
            swap(array,i,j);
        }
    }

    public static void main11(String[] args) {
        int[] array = {1, 3, 2, 5, 8, 7, 6, 3};

        func(array);

        System.out.println(Arrays.toString(array));
    }

    //改变原数组
    public static void transform1(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] *= 2;
        }
    }

    //不改变原数组
    public static int[] transform2(int[] array) {
        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = array[i] * 2;
        }
        return tmp;
    }

    public static void main10(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
//        transform1(array);
        int[] ret = transform2(array);
        System.out.println(Arrays.toString(array));
        System.out.println("ret: " + Arrays.toString(ret));

    }

    public static void main9(String[] args) {
        int[] array1 = {1, 21, 31, 3, 5};
        int[] array2 = {1, 21, 31, 3, 51};
        //比较两个数组是否相同的方法
        boolean flag = Arrays.equals(array1,array2);
        System.out.println(flag);

        System.out.println("=======fill=======");
        int[] array3 = new int[10];
        //填入元素的方法，可以指定范围
        //Arrays.fill(array3,6);
        Arrays.fill(array3,2,5,6);
        System.out.println(Arrays.toString(array3));
    }

    //暴力查找
    public static int find(int[] array,int key) {

        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return i;
            }
        }
        return -1;
    }

    //二分查找
    public static int binarySearch(int[] array, int key) {

        int left = 0;
        int right = array.length - 1;

        while(left <= right) {
            int mid = (left + right) / 2;
            if(array[mid] < key) {
                left = mid + 1;
            } else if (array[mid] == key) {
                return mid;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static void swap(int[] array ,int i, int j) {
        int tmp = array[j];
        array[j] = array [i];
        array[i] = tmp;
    }

    public static void bubbleSort(int[] array) {
        //  优化：有n个元素，则n-1次排序后就可以排好
        for (int i = 0; i < array.length - 1; i++) {
            //优化：用于判断数组元素是否进行了交换，没交换说明已排好，直接跳出
            boolean flag = false;
            //优化：- 1 - i 是因为后面的元素已排好！
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]) {
                    swap(array,j,j + 1);
                    flag = true;
                    //只要进行了交换，那就不跳出，再检查一次
                }
            }
            if(flag == false){
                break;
            }
        }
    }


    public static void main8(String[] args) {
        int[] array = {1, 21, 31, 3, 5};

//        bubbleSort(array);
//        Java自身提供了高效的排序！还可以选择排序的范围
//        Arrays.sort(array,2,5);
        Arrays.sort(array);

        System.out.println("排序后的数组：" + Arrays.toString(array));

//        int index = find(array,3);
//        int index = binarySearch(array,3);
//        Java也内置了数组的二分查找方法
        int index = Arrays.binarySearch(array,13);

        if(index == -1) {
            System.out.println("没找到！");
        } else {
            System.out.println("找到了，下标是" + index);
        }

    }

    public static void main7(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        //Java中，下标给定范围一般是[ )左开右闭区间
        //还可以指定范围复制！
        int[] copy = Arrays.copyOfRange(array,1,3);

        System.out.println(Arrays.toString(copy));
    }

    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        //如果复制的范围比原数组范围还大，其实是完成了扩容的操作
        //这是因为源码中实现的逻辑是用System.arraycopy()函数实现的，而复制范围的参数是选择
        //复制范围和原数组元素个数中较小的那个。
        int[] copy = Arrays.copyOf(array,array.length*2);
        array = copy;

        for (int x : copy) {
            System.out.println(x + " ");
        }
        System.out.println();

        System.out.println(Arrays.toString(array));
    }

    public static void main5(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        int[] copy = new int[array.length];
        //第一个参数是要复制的原型数组，第二个参数是从原型数组的第几个元素开始复制
        //第三个是复制到哪个数组，第四个是从哪个位置开始，第五个是复制多少个元素
        System.arraycopy(array,0,copy,0,array.length);

        for (int x : copy) {
            System.out.print(x + " ");
        }
        System.out.println();
    }

    //用Arrays方法快速地拷贝
    public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        int[] copy = Arrays.copyOf(array,array.length);

        for (int x : copy) {
            System.out.print(x + " ");
        }
        System.out.println();
    }


    //这不是拷贝，是两个引用变量指向同一个数组
    public static void main3(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int[] array2 = array;

        for(int x : array2) {
            System.out.print(x + " ");
        }
        System.out.println();
    }

    //拷贝一个数组（for循环版）
    public static void main2(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        int[] copy = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }

        for (int x : copy) {
            System.out.print(x + " ");
        }
        System.out.println();

    }

    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        System.out.println("============");
        for (int x : array) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("============");

        String ret = Arrays.toString(array);
        System.out.println(ret);
    }
}
